package com.sell.contorller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sell.entity.Members;
import com.sell.entity.Membertype;
import com.sell.service.MembersService;
import com.sell.service.MembertypeService;
import com.sell.util.Pager;

@Controller
@RequestMapping("/members")
public class MembersController {
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	@Resource
	MembersService membersService;
    @Resource
    MembertypeService membertypeService;
	/**
	 * 添加会员
	 */
	@RequestMapping(value = "/memberslist.html")
	public String memberlist(Model model,@RequestParam(value = "typeid", required = false) Integer typeid,
			@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "pageNo", required = false) String pageNoStr) {
		Integer num=0;
		int pageNo = 1;
		int tid=0;
		if (pageNoStr != null && !pageNoStr.equals("")) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		if(typeid!=null){
		   tid=typeid;
		}
		System.out.println(tid+">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		if(tid==0){
			if(name==null||name.equals("")){
				num=0;
			}else if(name!=null||!name.equals("")){
				num=2;
			}
		}else if(tid!=0){
			if(name==null||name.equals("")){
				num=1;
			}else{
				num=3;
			}
		}
		try {
			Pager pager=new Pager(membersService.getAllCount(tid, num, name), 7, pageNo);
			List<Members> members=membersService.getAllMembers(tid, num, name, pager);
			for(Members mem:members){
				Membertype memtype=membertypeService.getMemTypeById(mem.getTypeid());
		        mem.setMembertype(memtype);
			}
			List<Membertype> memtype=membertypeService.getAllMemType();
			for(Membertype mtype:memtype){
				System.out.println(mtype.getTypeId()+">>>>>>>>>>>>>>>>>>>>>.");
			}
			model.addAttribute("memtype",memtype);
			model.addAttribute("members",members);
			model.addAttribute("pager",pager);
			model.addAttribute("typeid",tid);
			model.addAttribute("name",name);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "kh-khgl";
	}
	@RequestMapping(value="/addmember.html")
	public String addmember(){
		return "gl-khgl-tc";
	}
	@RequestMapping(value="/savemember.html",method = RequestMethod.POST)
	public String savemember(String cardid,String name,String sex,@RequestParam("typeids")Integer typeids,String address,
			String birthday,String phone,String totalmoney){
		double money=Double.parseDouble(totalmoney);
		System.out.println(typeids);
		try {
			Date day=sdf.parse(birthday);
			int integral=(int) money;
			Members member=new Members();
			member.setAddress(address);
			member.setBirthday(day);
			member.setCardid(cardid);
			member.setIntegral(integral);
			member.setTypeid(typeids);
			member.setName(name);
			member.setPhone(phone);
			member.setSex(sex);
			member.setTotalmoney(money);
			membersService.addMember(member);
		} catch (Exception e) {
			e.printStackTrace();
			return "gl-khgl-tc";
		}
		return "redirect:/members/memberslist.html";
	}
	@RequestMapping(value="/updatemember.html")
	public String updatemember(Integer membersid,Model model){
		try {
			Members members=membersService.getMemberByid(membersid);
			model.addAttribute(members);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "updatemember";
	}
	@RequestMapping(value="/mergemember.html",method = RequestMethod.POST)
	public String mergemember(@RequestParam("name")String name,@RequestParam("sex")String sex,
			@RequestParam("address")String address,@RequestParam("birthday")String birthday,
			@RequestParam("phone")String phone,Integer membersid){
		try {
			Date day=sdf.parse(birthday);
			Members members=membersService.getMemberByid(membersid);
			System.out.println(members);
			System.out.println(name+">>>>>>>>>>");
			members.setName(name);
			System.out.println(sex+"--------------");
			members.setSex(sex);
			System.out.println(address+"11111111111111");
			members.setAddress(address);
			System.out.println(day+"22222222222");
			members.setBirthday(day);
			System.out.println(phone+"33333333");
			members.setPhone(phone);
			int num=membersService.updateAll(members);
		} catch (Exception e) {
			e.printStackTrace();
			return "updatemember";
		}
		return "redirect:/members/memberslist.html";
		
	}
}
